# ield 的实际应用场景
# 处理大型数据集：避免一次性加载所有数据到内存
# 无限序列：表示数学上的无限序列
# 管道处理：将多个生成器串联起来处理数据
# 状态机：保持函数的状态
# 协程和异步编程：在 asyncio 等框架中使用

# def simple_genertor():
#     yield 1
#     yield 2
#     yield 3
#
# gen = simple_genertor()
# print(gen)
# print(next(gen))
#
#
# def count_up_to(max):
#     count = 1
#     while count <= max:
#         yield count
#         count += 1
#
# counter = count_up_to(5)
#
# for num in counter:
#     print(num)



def accumulator():
    total = 0
    while True:
        value = yield total
        if value is None:
            break
        total += value

acc = accumulator()
next(acc)  # 启动生成器
print(acc.send(10))  # 输出: 10
print(acc.send(20))  # 输出: 30
print(acc.send(5))   # 输出: 35


def chain(*iterables):
    for it in iterables:
        yield from it

list(chain('ABC', 'DEF'))  # ['A', 'B', 'C', 'D', 'E', 'F']
